﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZK.Common;
using ZK.Common.ProModels;
using ZK.HardWareHelper;

namespace ZK.CoalYardTransManage
{
    /// <summary>
    /// 手动开关门（遥控开门改cs端开门， 并记录操作日志。）
    /// </summary>
    public partial class OpenClose : Form
    {
        private CameraHelper cameraHelperQB = new CameraHelper(1);
        private CameraHelper cameraHelperZB = new CameraHelper(1);

        public OpenClose()
        {
            InitializeComponent();


            var reQB =  cameraHelperQB.ManualOne(pictureBoxQB);
            if (!reQB.res)
            {
                MessageBox.Show(reQB.Msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            var reZB =cameraHelperZB.ManualOne(pictureBoxZB);
            if (!reZB.res)
            {
                MessageBox.Show(reQB.Msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            //OpenCamera();
            ///MessageBox.Show(cameraHelper.Login().Msg);
        }
        /// <summary>
        /// 初始化摄像头
        /// </summary>
        public void OpenCamera() {
            cameraHelperQB.ManualOne(pictureBoxQB);
            cameraHelperZB.ManualOne(pictureBoxZB);
        }

        /// <summary>
        /// 轻磅道闸开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {


            //上传记录
            var loginDto = WebApiService.TruckTare("zkkjadmin", "123456", "asd", 12, 34);

            string guid = Guid.NewGuid().ToString();
            ReBaseModel reBaseModel = cameraHelperQB.GetPucture(guid);//抓图
            if (!reBaseModel.res)
            {
                MessageBox.Show(reBaseModel.Msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            string filePathS = ImgHelper.CompressImg(sFile: reBaseModel.Msg);//压缩图片
            if (string.IsNullOrWhiteSpace(filePathS))
            {
                MessageBox.Show(TipsStringHelper.ystp, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            string errStr;
            string pictureId = WebApiService.HttpUploadFile(filePathS, out  errStr);//上传图片 到服务器
            if (!string.IsNullOrWhiteSpace(errStr))
            {
                MessageBox.Show(errStr, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            ReleaseModel<string> releaseModel = WebApiService.Release(flag: 1, imgUrl: pictureId);//手动放行道闸
            if (releaseModel == null || !releaseModel.success)
            {
                MessageBox.Show(releaseModel.msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            //开门
            cameraHelperQB.BarrierGateControl(cmdFlag: 1);
        }
        /// <summary>
        /// 轻磅道闸关
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            //关门
            cameraHelperQB.BarrierGateControl(cmdFlag:0);
        }
        /// <summary>
        /// 重磅道闸开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            //上传记录

            string guid = Guid.NewGuid().ToString();
            ReBaseModel reBaseModel = cameraHelperZB.GetPucture(guid);//抓图
            if (!reBaseModel.res)
            {
                MessageBox.Show(reBaseModel.Msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            string filePathS = ImgHelper.CompressImg(sFile: reBaseModel.Msg);
            if (string.IsNullOrWhiteSpace(filePathS))
            {
                MessageBox.Show(TipsStringHelper.ystp, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            string errStr;
            string pictureId = WebApiService.HttpUploadFile(filePathS, out  errStr);//上传图片 到服务器
            if (!string.IsNullOrWhiteSpace(errStr))
            {
                MessageBox.Show(errStr, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            ReleaseModel<string> releaseModel = WebApiService.Release(flag: 1, imgUrl: pictureId);//手动放行道闸
            if (releaseModel == null || !releaseModel.success)
            {
                MessageBox.Show(releaseModel.msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            //开门
            cameraHelperZB.BarrierGateControl(cmdFlag: 1);

        }
        /// <summary>
        /// 重磅道闸关
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            //关门
            cameraHelperZB.BarrierGateControl(cmdFlag: 0);
        }
        /// <summary>
        /// 窗体关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OpenClose_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing) {
                e.Cancel = true;
                this.WindowState = FormWindowState.Minimized;
            }
        }
        /// <summary>
        /// 右下角右击打开按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Activate();
            this.WindowState = FormWindowState.Maximized;
        }
        /// <summary>
        /// 右击右下角图标退出按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        /// <summary>
        /// 双击右下角图标显示界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            this.Activate();
            this.WindowState = FormWindowState.Maximized;
        }
        /// <summary>
        /// load事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OpenClose_Load(object sender, EventArgs e)
        {
            labelUserName.Text ="当前用户："+ AppSession.UserInfo.UserName;
        }
    }

}
